import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import axios from 'axios'
import VueAxios from 'vue-axios'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';

import { VueJsonp } from 'vue-jsonp'

Vue.use(VueAxios, axios)
Vue.config.productionTip = false
Vue.use(ElementUI);

Vue.use(VueJsonp)

//请求拦截器
axios.interceptors.request.use(config => {
  console.log('request config:', config.url)
  if (config.url == '/login' || config.url == '/register') {

  } else {
    //设置接口调用时配套的token值
    if (localStorage['token']) {
      axios.defaults.headers.common['Authorization'] = localStorage['token']
    }
  }
  return config
})

//响应拦截器
axios.interceptors.response.use(config => {
  //1、获取返回的状态码
  //2、潘判断状态码是否401
  //2.1是401，鉴权失败
  //2.1.1判断本地存储（localStorage.getItem（'token'））是否存在
  //2.1.2token存在时，删除过期的token，路由跳转到登录界面
  //2.2不是401，不处理
  console.log('response config:', config)
  return config
})

// //设置接口调用时配套的token值
// if (localStorage['token']) {
//   axios.defaults.headers.common['Authorization'] = localStorage['token']
// }

//设置接口地址公共部分
axios.defaults.baseURL = 'http://localhost:3000/'

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
